package com.kaifamiao.high.thread;

/**
 * @author lucien-ma
 * @date 2021/9/29
 * @apiNote
 */
public class Demo02 {
    public static void main(String[] args) {
        // 创建任务
        TicketRunnable ticketRunnable = new TicketRunnable();
        new Thread(ticketRunnable, "一号窗口").start();
        new Thread(ticketRunnable, "二号窗口").start();
        new Thread(ticketRunnable, "三号窗口").start();
        new Thread(ticketRunnable, "四号窗口").start();
        new Thread(ticketRunnable, "五号窗口").start();
    }
}


class TicketRunnable implements Runnable {
    private int num = 100;
    private Object lock = new Object();

    @Override
    public void run() {
        while (true) {
            if (num > 0) {
                synchronized (lock) {
                    if (num > 0) {
                        System.out.println(Thread.currentThread().getName()+"卖出第"+ num + "张票");
                        num --;
                    }
                }
                try {
                    Thread.currentThread().sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                break;
            }
        }

    }
}